<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="等级名称" prop="spuName">
        <el-input
          v-model="queryParams.spuName"
          placeholder="请输入等级名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="isOpen">
        <el-select v-model="queryParams.isOpen" placeholder="请选择状态" clearable class="!w-240px">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button type="primary" @click="openForm(undefined)" v-hasPermi="[createPermi]">
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="编号" align="center" prop="spuId" min-width="60" />
      <el-table-column label="等级图标" align="center" prop="picUrl" min-width="80">
        <template #default="{ row }">
          <el-image :src="row.picUrl" class="h-30px w-30px" @click="imagePreview(row.picUrl)" />
        </template>
      </el-table-column>
      <el-table-column label="等级背景图" align="center" prop="imgOblongUrl" min-width="80">
        <template #default="{ row }">
          <el-image
            :src="row.imgOblongUrl"
            class="h-30px w-30px"
            @click="imagePreview(row.imgOblongUrl)"
          />
        </template>
      </el-table-column>
      <el-table-column label="等级名称" align="center" prop="spuName" min-width="100" />
      <el-table-column label="有效期" align="center" prop="expireDateRange" min-width="110" />
      <el-table-column label="零售价" align="center" prop="priceRange" min-width="100" />
      <!-- <el-table-column label="会员人数" align="center" prop="discountPercent" min-width="110" /> -->
      <!-- <el-table-column label="状态" align="center" prop="isOpen" min-width="70">
        <template #default="{ row }">
          {{ row.isOpen }}
        </template>
      </el-table-column> -->
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        min-width="170"
      />
      <el-table-column label="操作" align="center" min-width="110px" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm(scope.row.spuId)"
            v-hasPermi="[updatePermi]"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.spuId)"
            v-hasPermi="[deletePermi]"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.pageNo"
      :total="total"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script setup lang="ts">
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import * as ProductSpuApi from '@/api/mall/product/spu'
import { createImageViewer } from '@/components/ImageViewer'
const { push } = useRouter() // 路由跳转

/** 会员等级管理 **/
defineOptions({ name: 'MemberPaidLevel' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据

const queryParams = ref({
  pageNo: 1,
  pageSize: 10,
  spuName: '',
  isOpen: null,
  spuType: 5
}) // 查询参数
const createPermi = ref('product:paidMember:create') //新增权限
const updatePermi = ref('product:paidMember:update') //修改权限
const deletePermi = ref('product:paidMember:delete') //删除权限

const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ProductSpuApi.getSpuPage(queryParams.value)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 新增或修改 */
const openForm = (spuId?: number) => {
  // 修改
  if (typeof spuId === 'number') {
    push({ name: 'ProductSpuPaidMemberEdit', params: { spuId } })
    return
  }
  // 新增
  push({ name: 'ProductSpuPaidMemberAdd' })
}
/** 删除按钮操作 */
const handleDelete = async (spuId: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ProductSpuApi.deleteSpu(spuId)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}
/** 商品图预览 */
const imagePreview = (imgUrl: string) => {
  createImageViewer({
    urlList: [imgUrl]
  })
}
/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
